//
//  detailsView.m
//  garbageclient
//
//  Created by Wang Wu on 11-8-11.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "detailsView.h"
#import "commondef.h"
#import "SBJson.h"

static int backType = 0;

@implementation detailsView

@synthesize delegate;

- (void)dealloc
{
    [navBar release];
    [webdatabuf release];
    [results release];
    [textview release];
    [downloadbtn release];
    
    [super dealloc];
}

- (id)initWithUrl:(NSString*)strurl
{
    CGRect screenrc = [[UIScreen mainScreen] applicationFrame];
    screenrc.origin.y = 0;
    
    self = [super initWithFrame:screenrc];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        CGRect screenrc = [[UIScreen mainScreen] applicationFrame];
        //bar
        navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, screenrc.size.width, NAV_HEIGHT)];
        
        [self addSubview: navBar];
        
        UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle: @"详情"];
        UIBarButtonItem *rightbtn = [[UIBarButtonItem alloc] initWithTitle:@"评论" 
                                                                     style:UIBarButtonItemStylePlain
                                                                    target:self 
                                                                    action:@selector(comment:)];
        navItem.rightBarButtonItem = rightbtn;
        
        UIBarButtonItem *leftbtn = [[UIBarButtonItem alloc] initWithTitle:@"返回" 
                                                                     style:UIBarButtonItemStylePlain
                                                                    target:self 
                                                                    action:@selector(backtomainview:)];
        navItem.leftBarButtonItem = leftbtn;
        navItem.leftBarButtonItem.enabled = NO;
        [navBar pushNavigationItem:navItem animated:YES];
        
        [navItem release];
        [rightbtn release];
        
        //testview
        textview = [[UITextView alloc]initWithFrame:CGRectMake(0.0f, NAV_HEIGHT, screenrc.size.width, 380.0f)];
        textview.editable = NO;
        textview.textAlignment = UITextAlignmentCenter;
        textview.textColor = [UIColor brownColor];
        [self addSubview:textview];
        //download button
        downloadbtn = [[UIButton alloc] initWithFrame:CGRectMake(0.0f,screenrc.size.height - BTN_HEIGHT, BTN_WIDTH, BTN_HEIGHT)];// 
        [downloadbtn setTitle:@"下载" forState:UIControlStateNormal];
        [downloadbtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; 
        [downloadbtn addTarget:self action:@selector(downLoad:) forControlEvents:UIControlEventTouchUpInside];
        downloadbtn.enabled = NO;
        [self addSubview:downloadbtn];
        
        webdatabuf = [[NSMutableData alloc] initWithLength:0];
        
        NSURL *url = [NSURL URLWithString:strurl];
        NSLog(@"%@",strurl);
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url];
        connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        
        [request release];
    }
    return self;
}

-(void)comment:(id)sender
{
    CGRect screenrc = [[UIScreen mainScreen] applicationFrame];
    screenrc.origin.y = 0;
    
    commentview = [[commentView alloc] initWithFrame:screenrc];
    commentview.delegate = self;
    [self addSubview:commentview];
}

-(void)backtomainview:(id)sender
{
    [delegate detailback];
}

#pragma marks -func
-(void)setBackType:(int)iType
{
    backType = iType;
}

-(void)ConnectDetails
{
    
}

#pragma mark- connect
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{
    [webdatabuf appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)aConn
{
	NSString *jsonString = [[NSString alloc] initWithData:webdatabuf encoding:NSUTF8StringEncoding];
    
    [webdatabuf release];
    webdatabuf = [[NSMutableData alloc] initWithLength:0];
    
    [results release];
	results = [[jsonString JSONValue] retain]; 
    
    [jsonString release];
    [connection release];
    
    textview.text = [[NSString alloc] initWithFormat:@"%@\n\n%@",
                     [results objectForKey:@"Title"],
                     [((NSString *)[results objectForKey:@"Description1"]) stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"]];
    
    downloadurl = [results objectForKey:@"Link"];
    downloadbtn.enabled = YES;
    
    navBar.topItem.leftBarButtonItem.enabled = YES;
}

#pragma mark-
-(void)downLoad:(id)sender
{
    NSURL *url = [NSURL URLWithString:downloadurl]; 
    [[UIApplication sharedApplication]openURL:url];
}

#pragma mark- touchevent
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
}

#pragma mark- commentview delegate
-(void)commentback
{
    [commentview removeFromSuperview];
    [commentview release];
}
@end
